package com.ruoyi.business.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysDeptMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.CzAircraftMapper;
import com.ruoyi.business.domain.CzAircraft;
import com.ruoyi.business.service.ICzAircraftService;

/**
 * 航空器登记Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-01-01
 */
@Service
public class CzAircraftServiceImpl implements ICzAircraftService 
{
    @Autowired
    private CzAircraftMapper czAircraftMapper;
    @Autowired
    private SysDeptMapper deptMapper;

    /**
     * 查询航空器登记
     * 
     * @param id 航空器登记主键
     * @return 航空器登记
     */
    @Override
    public CzAircraft selectCzAircraftById(String id)
    {
        return czAircraftMapper.selectCzAircraftById(id);
    }

    /**
     * 查询航空器登记列表
     * 
     * @param czAircraft 航空器登记
     * @return 航空器登记
     */
    @Override
    public List<CzAircraft> selectCzAircraftList(CzAircraft czAircraft)
    {
        return czAircraftMapper.selectCzAircraftList(czAircraft);
    }

    /**
     * 新增航空器登记
     * 
     * @param czAircraft 航空器登记
     * @return 结果
     */
    @Override
    public int insertCzAircraft(CzAircraft czAircraft)
    {
    	SysUser user = SecurityUtils.getLoginUser().getUser();
    	SysDept dept= deptMapper.selectDeptById(user.getDeptId());
    	//审核人员为本部门负责人员
    	czAircraft.setAuditUserId(dept.getLeader());
    	czAircraft.setRegistrationStatus("apply");
        czAircraft.setCreateTime(DateUtils.getNowDate());
        return czAircraftMapper.insertCzAircraft(czAircraft);
    }

    /**
     * 修改航空器登记
     * 
     * @param czAircraft 航空器登记
     * @return 结果
     */
    @Override
    public int updateCzAircraft(CzAircraft czAircraft)
    {
    	if("approval".equals(czAircraft.getRegistrationStatus())) {
    		czAircraft.setSerialNo(generaSerialNo());
    		czAircraft.setRegistrationTime(DateUtils.getNowDate());
    	}
        czAircraft.setUpdateTime(DateUtils.getNowDate());
        return czAircraftMapper.updateCzAircraft(czAircraft);
    }

    /**
     * 批量删除航空器登记
     * 
     * @param ids 需要删除的航空器登记主键
     * @return 结果
     */
    @Override
    public int deleteCzAircraftByIds(String[] ids)
    {
        return czAircraftMapper.deleteCzAircraftByIds(ids);
    }

    /**
     * 删除航空器登记信息
     * 
     * @param id 航空器登记主键
     * @return 结果
     */
    @Override
    public int deleteCzAircraftById(String id)
    {
        return czAircraftMapper.deleteCzAircraftById(id);
    }
    
	public String generaSerialNo() {
		DateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date nowDate = DateUtils.getNowDate();
		StringBuffer serialNo = new StringBuffer();
		serialNo.append("UAV").append(sdf.format(nowDate))
				.append(String.format("%04d", czAircraftMapper.selectCountByDate(nowDate) + 1));
		return serialNo.toString();
	}
}
